package com.atguigu.es.client;

import com.atguigu.es.beans.Emp;
import io.searchbox.action.Action;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.search.aggregation.AvgAggregation;
import io.searchbox.core.search.aggregation.MetricAggregation;
import io.searchbox.core.search.aggregation.TermsAggregation;

import java.io.IOException;
import java.util.List;

/**
 * Created by Smexy on 2022/5/21
 *
 *
 *
 *       Action: 要执行的命令
 *              读: Search
 *
 *
 */
public class ReadDemo1 {

    public static void main(String[] args) throws IOException {

        //创建客户端
        JestClientFactory jestClientFactory = new JestClientFactory();

        // 连接server端
        HttpClientConfig httpClientConfig = (new HttpClientConfig.Builder("http://hadoop102:9200")).build();

        jestClientFactory.setHttpClientConfig(httpClientConfig);

        JestClient jestClient = jestClientFactory.getObject();

        String requestBody="{\n" +
                "  \"query\": {\n" +
                "    \"match\": {\n" +
                "      \"hobby\": \"购物\"\n" +
                "    }\n" +
                "  }, \n" +
                "  \"aggs\": {\n" +
                "    \"gendercount\": {\n" +
                "      \"terms\": {\n" +
                "        \"field\": \"gender.keyword\",\n" +
                "        \"size\": 2\n" +
                "      }\n" +
                "    },\n" +
                "    \"avgage\":{\n" +
                "      \"avg\": {\n" +
                "        \"field\": \"age\"\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}";
        // 准备操作的命令
        Search search = new Search.Builder(requestBody).addIndex("test").addType("emps").build();


        // 执行命令
        SearchResult searchResult = jestClient.execute(search);

        // 如果是读，解析server返回的结果
        System.out.println("total:"+searchResult.getTotal());
        System.out.println("max_score:"+searchResult.getMaxScore());

        List<SearchResult.Hit<Emp, Void>> hits = searchResult.getHits(Emp.class);

        for (SearchResult.Hit<Emp, Void> hit : hits) {

            System.out.println("_index"+hit.index);
            System.out.println("_type"+hit.type);
            System.out.println("_id"+hit.id);
            System.out.println("_score"+hit.score);
            System.out.println(hit.source);

        }

        //获取聚合
        MetricAggregation aggregations = searchResult.getAggregations();

        TermsAggregation gendercount = aggregations.getTermsAggregation("gendercount");

        for (TermsAggregation.Entry bucket : gendercount.getBuckets()) {

            System.out.println(bucket.getKey() + ":" + bucket.getCount());

        }

        AvgAggregation avgage = aggregations.getAvgAggregation("avgage");

        System.out.println(avgage.getAvg());

        // 关闭客户端
        jestClient.close();

    }


}
